//
//  SplitModeViewController.m
//  TextToImg
//
//  Created by Garenge on 2019/M/29.
//  Copyright © 2019 鹏鹏. All rights reserved.
//

#import "SplitModeViewController.h"
#import <Masonry/Masonry.h>
#import "AppTool.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "UILabel+text.h"
#import "UIColor+color.h"

@interface SplitModeViewController ()

@property (nonatomic, strong) UIButton *topSeleBtn;
@property (nonatomic, strong) UITextField *topTextFiled;

@property (nonatomic, strong) UIButton *botSeleBtn;
@property (nonatomic, strong) UITextField *botTextFiled;
@property (nonatomic, strong) UISwitch *customSpliSwitch;

@end

@implementation SplitModeViewController

- (SplitMode)splitMode {
    if (_splitMode > 0) {
        return _splitMode;
    } else {
        _splitMode = [[AppTool sharedAppTool] getSpliteMode];
        return _splitMode;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"分页模式";
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(saveSpliteModeInfo:)];
    self.navigationItem.rightBarButtonItems = @[rightItem];
    
    [self setUpSubviews];
    
    if (self.splitMode == SplitModeWithString) {
        self.topSeleBtn.selected = YES;
        self.botSeleBtn.selected = NO;
    } else if (self.splitMode == SplitModeWithCount) {
        self.topSeleBtn.selected = NO;
        self.botSeleBtn.selected = YES;
    }
}

- (void)setUpSubviews {
    // bgView
    UIView *topBgView = [[UIView alloc] init];
    topBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topBgView];
    
    [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(20);
        make.left.equalTo(self.view).with.offset(20);
        make.right.equalTo(self.view).with.offset(-20);
    }];
    
    UITapGestureRecognizer *topTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topTapAction:)];
    [topBgView addGestureRecognizer:topTap];
    
    topBgView.layer.cornerRadius = 6;
    topBgView.layer.borderWidth = 0.5;
    topBgView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    // select button
    UIButton *topSeleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [topSeleBtn setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
    [topSeleBtn setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
    topSeleBtn.userInteractionEnabled = NO;
    [topBgView addSubview:topSeleBtn];
    self.topSeleBtn = topSeleBtn;
    
    [topSeleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBgView).with.offset(15);
        make.left.equalTo(topBgView).with.offset(20);
        make.size.mas_equalTo(CGSizeMake(24, 24));
        make.bottom.equalTo(topBgView).with.offset(-15);
    }];
    
    // title
    UILabel *topTitleLabel = [[UILabel alloc] init];
    topTitleLabel.font = [UIFont systemFontOfSize:15];
    topTitleLabel.text = @"按字符分页";
    [topBgView addSubview:topTitleLabel];
    
    [topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topSeleBtn.mas_right).with.offset(10);
        make.centerY.equalTo(topSeleBtn);
        make.size.mas_equalTo(CGSizeMake(80, 21));
    }];
    
    // value
    UITextField *topTextField = [[UITextField alloc] init];
    topTextField.font = [UIFont systemFontOfSize:15];
    topTextField.textAlignment = NSTextAlignmentRight;
    topTextField.borderStyle = UITextBorderStyleRoundedRect;
    topTextField.placeholder = @"请输入分割符, 比如\"====\"";
    topTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    NSString *spliteString = [[AppTool sharedAppTool] getSplitString];
    topTextField.text = spliteString;
    [topBgView addSubview:topTextField];
    self.topTextFiled = topTextField;
    
    [topTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topTitleLabel.mas_right).with.offset(25);
        make.centerY.equalTo(topSeleBtn);
        make.right.equalTo(topBgView).with.offset(-15);
        make.height.mas_equalTo(30);
//        make.width.mas_equalTo(110);
    }];
    
    
    // bgView
    UIView *botBgView = [[UIView alloc] init];
    botBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:botBgView];
    
    [botBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBgView.mas_bottom).with.offset(15);
        make.left.equalTo(self.view).with.offset(20);
        make.right.equalTo(self.view).with.offset(-20);
    }];
    
    UITapGestureRecognizer *botTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(botTapAction:)];
    [botBgView addGestureRecognizer:botTap];
    
    botBgView.layer.cornerRadius = 6;
    botBgView.layer.borderWidth = 0.5;
    botBgView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    // select button
    UIButton *botSeleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [botSeleBtn setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
    [botSeleBtn setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
    botSeleBtn.userInteractionEnabled = NO;
    [botBgView addSubview:botSeleBtn];
    self.botSeleBtn = botSeleBtn;
    
    [botSeleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(botBgView).with.offset(15);
        make.left.equalTo(botBgView).with.offset(20);
        make.centerY.equalTo(botBgView);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    
    // title
    UILabel *botTitleLabel = [[UILabel alloc] init];
    botTitleLabel.font = [UIFont systemFontOfSize:15];
    botTitleLabel.text = @"按字数分页";
    [botBgView addSubview:botTitleLabel];
    
    [botTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(botBgView).with.offset(15);
        make.left.equalTo(botSeleBtn.mas_right).with.offset(10);
//        make.centerY.equalTo(botSeleBtn);
        make.size.mas_equalTo(CGSizeMake(80, 21));
    }];
    
    // value
    UITextField *botTextField = [[UITextField alloc] init];
    botTextField.font = [UIFont systemFontOfSize:15];
    botTextField.textAlignment = NSTextAlignmentRight;
    botTextField.borderStyle = UITextBorderStyleRoundedRect;
    botTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    botTextField.placeholder = [NSString stringWithFormat:@"%ld, 最大99999", (long)[[AppTool sharedAppTool] getSpliteTextCount]];
    [botBgView addSubview:botTextField];
    self.botTextFiled = botTextField;
    
    [botTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(botTitleLabel.mas_right).with.offset(25);
        make.centerY.equalTo(botTitleLabel);
        make.right.equalTo(botBgView).with.offset(-15);
        make.height.mas_equalTo(30);
//        make.width.mas_equalTo(110);
    }];

    // 智能分页
    UILabel *customSpliLabel = [[UILabel alloc] init];
    customSpliLabel.font = [UIFont systemFontOfSize:15];
    customSpliLabel.text = @"智能分页";
    [botBgView addSubview:customSpliLabel];

    [customSpliLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(botTitleLabel.mas_bottom).with.offset(10);
        make.left.equalTo(botTitleLabel);
        make.size.mas_equalTo(CGSizeMake(80, 21));
        make.bottom.equalTo(botBgView).with.offset(-15);
    }];

    // switch
    UISwitch *customSpliSwitch = [[UISwitch alloc] init];
    customSpliSwitch.on = [[AppTool sharedAppTool] getCustomSplite]; // 默认是开
    [botBgView addSubview:customSpliSwitch];
    self.customSpliSwitch = customSpliSwitch;

    [customSpliSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(botBgView).with.offset(-15);
        make.centerY.equalTo(customSpliLabel);
    }];

    customSpliSwitch.onTintColor = [UIColor colorWithLongHex:0x43b2f4];
    CGAffineTransform customSpliSwitchform = CGAffineTransformMakeScale(0.8f, 0.8f);
    customSpliSwitch.transform = customSpliSwitchform;//设定宽高

    [self.view layoutIfNeeded];
    [topTitleLabel changeAlignmentRightandLeft];
    [botTitleLabel changeAlignmentRightandLeft];
    [customSpliLabel changeAlignmentRightandLeft];

    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightLight];
    tipLabel.numberOfLines = 0;
    NSString *tipsStr = @"帮助:\n\t1. 当内容过多的时候建议分割成多图显示\n\t2. app支持如上两种方式, 点击按钮即可切换\n\t3. 输入框默认显示的是上一次的设置\n\t4. 当选中\"按字符分页\"时, \n\t\t如果不输入任何内容, 将不会分页\n\t5. 当选中\"按数字分割\"时,\n\t\ta. app保存的时候将数字限制在0~99999内\n\t\tb. 如果输入\"0\", 将不会分页\n\t\tc. 默认勾选智能分页\n\t\t\t自动寻找前后最近的段落处截取\n\t6. 记得点击右上角的确定按钮";
    NSMutableAttributedString *tipsAttributedStr = [[NSMutableAttributedString alloc] initWithString:tipsStr];

    NSMutableDictionary *tipsMDic = [NSMutableDictionary dictionary];
    [tipsMDic setObject:[UIFont systemFontOfSize:14 weight:UIFontWeightLight] forKey:NSFontAttributeName];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];
    [tipsMDic setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    
    [tipsAttributedStr setAttributes:tipsMDic range:NSMakeRange(0, tipsStr.length)];
    tipLabel.attributedText = tipsAttributedStr;
    [self.view addSubview:tipLabel];
    
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(botBgView.mas_bottom).with.offset(15);
        make.left.equalTo(self.view).with.offset(25);
        make.right.equalTo(self.view).with.offset(-25);
    }];
}

- (void)topTapAction:(id)sender {
    self.topSeleBtn.selected = YES;
    self.botSeleBtn.selected = NO;
    
    self.splitMode = SplitModeWithString;
}

- (void)botTapAction:(id)sender {
    self.botSeleBtn.selected = YES;
    self.topSeleBtn.selected = NO;
    
    self.splitMode = SplitModeWithCount;
}

- (void)saveSpliteModeInfo:(id)sender {
    // 先检查
    // 后保存
    if (self.splitMode == SplitModeWithString) {
        
        NSString *setContent = self.topTextFiled.text;
        [[AppTool sharedAppTool] saveSplitMode:self.splitMode];
        [[AppTool sharedAppTool] saveSplitString:setContent];
        
    } else if (self.splitMode == SplitModeWithCount) {
        
        NSString *setContent = self.botTextFiled.text;
        if (setContent.length == 0) {
            if ([[AppTool sharedAppTool] getSpliteTextCount] > 0) {
                //
            } else {
                [SVProgressHUD showErrorWithStatus:@"请输入设置内容"];
                return;
            }
        } else {
            if (![self isPureInt:setContent]) {
                [SVProgressHUD showErrorWithStatus:@"请输入数字"];
                return;
            } else {
                [[AppTool sharedAppTool] saveSplitTextCount:[setContent integerValue]];
            }
        }
        [[AppTool sharedAppTool] saveCustomSplite:self.customSpliSwitch.on];
        [[AppTool sharedAppTool] saveSplitMode:self.splitMode];
    }
    
    [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    
    if (self.completeHander) {
        self.completeHander(self.splitMode);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)isPureInt:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}

@end
